<script setup>
import { userLoginService } from '@/api/user.js'
import { useUserStore } from '@/stores/index'
import { useRouter } from 'vue-router'
import { User, Lock } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { ref } from 'vue'
const form = ref()
// const isRegister = ref(true)
//整个的用于提交爱的form数据对象
const formModel = ref({
  username: 'qwerty',
  password: 'qwerty'
})
//整个表单的校验规则
//1.非空校验  required: true, 消息提醒 message: '请输入用户名', 触发校验的时机  trigger blur 表示失焦时触发，change是值改变触发
//2.正则校验 pattern: 正则规则 \S 非空字符
const rules = {
  username: [
    {
      required: true,
      message: '请输入用户名',
      trigger: 'blur'
    },
    { min: 4, max: 10, message: '用户名必须是5-10位的字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{5,15}$/,
      message: '密码必须是6-15位的非空字符',
      trigger: 'blur'
    }
  ]
}
const userStore = useUserStore()
const router = useRouter()
const login = async () => {
  await form.value.validate()
  const res = await userLoginService(formModel.value)
  if(res.code==200){

    userStore.setToken(res.token)
    userStore.setinfo(res)
  console.log('uuuu',userStore.userinfo)
  
  ElMessage({
    message:'登录成功',
    type:'success'
  })
  router.push('/')
  }
  else {
    ElMessage.console.error(res.data.msg);
  }
  
  // console.log('开始登录', res)
}
</script>
<template>
  <!-- 1.静态样式 -->
  <!-- el-row 表示一行，一行分成24份 -->
  <!-- el-col 表示列
    (1):span='12' 代表一行中占12份（50%）
    (2):span='6' 代表一行中占6份（25%）
    (3):offset="3" 代表一行中，左侧的margin份数
    
    el-from 整个表单组件
    el-from-item 表单的一行（一个表单域）
    el-input 表单元素（输入框）
  -->
  <!-- 2.校验相关 -->
  <!-- (1)el-form => :model="ruleFrom" 绑定的整个form的数据对象 
       (2)el-form => :rules='rules' 绑定的整个form规则对象
       (3)表单元素 => v-model='ruleForm.XXX' 给表单元素，绑定form的子属性
       (4)el-form-item => prop配置生效的是哪个校验规则（和rules中的字段对应）
  -->
  <el-row class="login-page">

    <el-col :span="6" class="form">
      <!-- 登录相关表单 -->
      <el-form
        :model="formModel"
        :rules="rules"
        ref="form"
        size="large"
        autocomplete="off"
      >
        <el-form-item >
         <img style=" height: 120px;width: 240px; margin: 0 auto;" src="@/assets/yuesao.png" alt="">
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            v-model="formModel.username"
            :prefix-icon="User"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formModel.password"
            name="password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item class="flex">
          <div class="flex">
            <el-checkbox>记住我</el-checkbox>
            <el-link type="primary" :underline="false">忘记密码？</el-link>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button
            @click="login"
            class="button"
            type="primary"
            auto-insert-space
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>
<style lang="scss" scoped>
.login-page {
  height: 100vh;
  width: 100vw;
  background-image: url(@/assets/loginShow.jpg);
  background-size: cover;
  background-position: center;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  .form {
    display: flex;
    min-width: 300px;
    max-width: 400px;
    padding:30px 15px 20px;
    margin-right: 280px;
    flex-direction: column;
    justify-content: center;
    user-select: none;
    border-radius: 2%;
    background-color: rgba($color:#fff, $alpha: 0.8);
    .title {
      margin: 0 auto;
    }
    .button {
      width: 100%;
    }
    .flex {
      width: 100%;
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>
